package com.marketManage.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedList;
import java.util.Scanner;

import com.adv.io.Per;

public class Test {

	public static void main(String[] args) throws Exception {
		Scanner input = new Scanner(System.in);
		// 反序列化
		ObjectInputStream oint = new ObjectInputStream(new FileInputStream((new File("Market.txt"))));
		MarketMag sp = (MarketMag) oint.readObject();
		oint.close();
		if (sp == null) {
			sp = new MarketMag();
		}

		while (true) {
			System.out.println(
					"****************************************************************************************");
			System.out.println("1、开卡  \t2、修改密码\t3、积分累计\t4、积分兑换\t5、查询剩余积分\t6、退出");
			System.out.println(
					"****************************************************************************************");
			System.out.print("请选择：");
			int num = input.nextInt();
			if (num == 1) {// 1、开卡
				System.out.print("请输入注册姓名：");
				String name = input.next();
				String password;
				boolean flag = false;
				do {
					System.out.print("请输入注册密码：");
					password = input.next();
					flag = sp.cheakPassword(password);
				} while (!flag);
				if (sp.openCard(name, password)) {
					System.out.println("恭喜您开卡成功！赠送您100积分！您的会员卡号是：" + sp.list.get(0).getCardNumber());
				} else {
					System.out.println("注册失败！");
				}

			} else if (num == 2) {// 2、修改密码
				System.out.print("请输入您的会员卡号：");
				int cardNum = input.nextInt();
				System.out.print("请输入您的会员卡密码：");
				String oldPS = input.next();
				System.out.print("请输入您的新密码：");
				String newPS = input.next();
				if (sp.changePassword(cardNum, oldPS, newPS)) {
					System.out.println("密码修改成功！");
				} else {
					System.out.println("密码修改失败！");
				}

			} else if (num == 3) {// 3、积分累计
				System.out.print("请输入您的会员卡号：");
				int cardNum = input.nextInt();
				System.out.print("请输入您的会员卡密码：");
				String PS = input.next();
				System.out.print("请输入您本次的消费金额：");
				int money = input.nextInt();
				Member m = sp.addIntegral(cardNum, PS, money);
				if (m == null) {
					System.out.println("积分累计失败！");
				} else {
					System.out.println("姓名\t会员卡号\t\t剩余积分\t开卡日期");
					System.out.println(m.getName() + "\t" + m.getCardNumber() + "\t" + m.getIntegral() + "\t"
							+ sp.getDate(m.getCardOpenDate()));
				}

			} else if (num == 4) {// 4、积分兑换
				System.out.print("请输入您的会员卡号：");
				int cardNum = input.nextInt();
				System.out.print("请输入您的会员卡密码：");
				String PS = input.next();
				System.out.print("请输入您需要兑换使用的积分（100积分抵用0.1元，不足100积分不得使用）：");
				int integral = input.nextInt();

				Member m = sp.exIntegral(cardNum, PS, integral);//
				if (m == null) {
					System.out.println("积分兑换失败！");
				} else {
					double d1 = integral / 100;
					System.out.println("您的消费金额中使用会员积分抵扣：" + d1 + "元");
					System.out.println("积分兑换成功！");
					System.out.println("姓名\t会员卡号\t\t剩余积分\t开卡日期");
					System.out.println(m.getName() + "\t" + m.getCardNumber() + "\t" + m.getIntegral() + "\t"
							+ sp.getDate(m.getCardOpenDate()));
				}

			} else if (num == 5) {// 5、查询剩余积分
				System.out.print("请输入您的会员卡号：");
				int cardNum = input.nextInt();
				System.out.print("请输入您的会员卡密码：");
				String PS = input.next();
				Member m = sp.showintegral(cardNum, PS);
				if (m == null) {
					System.out.println("您输入的卡号或者密码错误！");
				} else {
					System.out.println("姓名\t会员卡号\t\t剩余积分\t开卡日期");
					System.out.println(m.getName() + "\t" + m.getCardNumber() + "\t" + m.getIntegral() + "\t"
							+ sp.getDate(m.getCardOpenDate()));
				}

			} else if (num == 6) {// 6、退出
				System.out.println("系统已退出，谢谢使用！");
				// 序列化
				ObjectOutputStream oout = new ObjectOutputStream(new FileOutputStream(new File("Market.txt")));
				oout.writeObject(sp);
				oout.close();
				return;

			} else {// 输入有误
				System.err.println("您的操作有误，请重新选择！");
			}
		}

	}

}
